<template>
	<view>
		<!--二维码跳转页面-->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				result: '',
				merchantsId: '',
			};
		},
		onLoad() {
			var me = this;
			uni.scanCode({
				success: (res) => {
					console.log(res)
					var path = res.path
					var scene = path.substring(path.indexOf('scene=') + 6)
					console.log(scene)
					//存二维码内容缓存
					uni.setStorageSync('content', scene)
					uni.request({
						url: me.$ServerUrl +
							'/invoice/api/qrcode/getMerchantIdByContent', //根据二维码内容获取二维码信息
						data: {
							content: scene
						},
						header: {
							'content-type': 'application/json'
						},
						method: "GET",
						success: (res) => {
							console.log(res.data.bindState)
							console.log("------------hh--------------")
							//qrcodeState 表示二维码状态，判断是否有商家绑定
							var qrcodeState = res.data.bindState
							//不存在二维码id则二维码不存在
							if (!res.data.id) {
								uni.showModal({
									content: '二维码已失效',
									showCancel: false,
									success() {
										uni.redirectTo({
											url: '../index',
										})
									}
								});

							} else {
								if (qrcodeState == 1) {
									uni.showModal({
										title: '提示',
										content: '二维码已被绑定',
										showCancel: false,
										success: function(res) {
											if (res.confirm) {
												//跳转到商家首页
												uni.redirectTo({
													url: '../index',
												})
												console.log('用户点击确定');
											} else if (res.cancel) {
												console.log('用户点击取消');
												//跳转到商家首页
												uni.redirectTo({
													url: '../index',
												})
											}
										}
									});


								} else {
									var qrcodeId = res.data.id
									console.log("二维码ID=" + qrcodeId)
									var merchantInfo = uni.getStorageSync("merchantInfo").id
									console.log('商家ID=' + JSON.stringify(merchantInfo))
									uni.request({
										url: me.$ServerUrl +
											'/invoice/api/qrcode/bindingQrcode', //绑定二维码
										data: {
											merchantId: merchantInfo,
											qrcodeId: qrcodeId
										},
										header: {
											'content-type': 'application/x-www-form-urlencoded'
										},
										method: "POST",
										success: (res) => {
											console.log('二维码内容' + JSON.stringify(res))
											uni.showModal({
												content: '提交成功',
												showCancel: false,
												success() {
													uni.redirectTo({
														url: './index',
													})
												}
											});
										}
									})
								}


							}

						}
					})
				},
			})

		},
		methods: {}
	}
</script>


<style>
	.scan-result {
		min-height: 50rpx;
		line-height: 50rpx;
	}
</style>
